package com.tim.app.ui.activity.setting;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.application.library.net.StringResponseCallback;
import com.application.library.runtime.event.EventListener;
import com.application.library.runtime.event.EventManager;
import com.application.library.widget.RatioImageView;
import com.google.gson.Gson;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.constant.EventTag;
import com.tim.app.manager.ImagePickerManager;
import com.tim.app.server.api.API_App;
import com.tim.app.server.api.API_User;
import com.tim.app.server.entry.User;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.server.result.UploadResult;
import com.tim.app.ui.activity.BaseActivity;
import com.tim.app.util.BitmapLoader;
import com.tim.app.util.ToastUtil;

/**
 * 编辑头像
 */
public class EditAvatarActivity extends BaseActivity {

    private static final String TAG = "EditAvatarActivity";

    private ImageButton ibBack;
    private TextView tvCamera;
    private TextView tvSelectPic;
    private RatioImageView rivAvatar;

    private User currentUser;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_avatar;
    }

    @Override
    public void initView() {
        ibBack = (ImageButton) findViewById(R.id.ibBack);
        tvCamera = (TextView) findViewById(R.id.tvCamera);
        tvSelectPic = (TextView) findViewById(R.id.tvSelectPic);
        rivAvatar = (RatioImageView) findViewById(R.id.rivAvatar);
        ibBack.setOnClickListener(this);
        tvCamera.setOnClickListener(this);
        tvSelectPic.setOnClickListener(this);
        rivAvatar.setOnClickListener(this);

        currentUser = (User) getIntent().getSerializableExtra(AppKey.USER);
        EventManager.ins().registListener(EventTag.NOTIFY_CHOOSE_PHOTO, eventListener);
    }

    public static void start(Context context, User user) {
        Intent intent = new Intent(context, EditAvatarActivity.class);
        intent.putExtra(AppKey.USER, user);
        context.startActivity(intent);
    }

    @Override
    public void initData() {
        if (null != currentUser) {
            if (!TextUtils.isEmpty(currentUser.getImage())) {
                BitmapLoader.ins().loadImage(currentUser.getImage(), rivAvatar);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventManager.ins().removeListener(EventTag.NOTIFY_CHOOSE_PHOTO, eventListener);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private EventListener eventListener = new EventListener() {
        @Override
        public void handleMessage(int what, int arg1, int arg2, Object dataobj) {
            if (what == EventTag.NOTIFY_CHOOSE_PHOTO) {
                if (dataobj != null) {
                    String path = dataobj.toString();
                    BitmapLoader.ins().loadLocalImage(path, rivAvatar, 700, 700);
                    updateUserAvatar(path);
                } else {
                    if (arg1 != AppKey.CODE_CANCEL) {
                        ToastUtil.showToast(getString(R.string.error_pic_get));
                    }
                }
            }
        }
    };

    /**
     * 修改用户头像
     *
     * @param path
     */
    private void updateUserAvatar(String path) {
        showLoadingDialog();
        API_App.ins().uploadAvatarData(TAG, path, new StringResponseCallback() {
            @Override
            public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                if (errCode == ResponseResult.ResultStatus.OK) {
                    UploadResult result = new Gson().fromJson(resultValue, UploadResult.class);
                    if (null != result && null != result.getRes() && null != result.getRes().getHashmap()) {
                        String url = result.getRes().getHashmap().getImgurl();
                        API_User.ins().modtifyUserAvatar(TAG, url, new StringResponseCallback() {
                            @Override
                            public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                                hideLoadingDialog();
                                if (errCode == ResponseResult.ResultStatus.OK) {
                                    EventManager.ins().sendEvent(EventTag.ACCOUNT_UPDATE_INFO, 0, 0, 0);
                                    ToastUtil.showToast("修改成功");
                                } else {
                                    ToastUtil.showToast(errMsg);
                                }
                                return false;
                            }
                        });
                    }
                } else {
                    hideLoadingDialog();
                    ToastUtil.showToast(errMsg);
                }
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvCamera:// 照相
                ImagePickerManager.ins().takePhoto(EditAvatarActivity.this, true);
                break;
            case R.id.tvSelectPic://选择图片
                ImagePickerManager.ins().pickPhoto(EditAvatarActivity.this, true);
                break;
            case R.id.ibBack:
                onBackPressed();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


}
